package com.code.demo.ui.common;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.code.demo.R;
import com.code.demo.util.DensityUtil;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;


/**
 * 显示底部对话框
 */
public class ActionSheet extends Dialog
{

    private static ActionSheet customDialog;

    public ActionSheet(Context context, int theme)
    {
        super(context, theme);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
    }

    /**
     * 初始化
     * @param context:上下文
     * @return
     */
    public static ActionSheet createDialog(Context context)
    {
        customDialog = new ActionSheet(context, R.style.ActionSheetDialogTheme);
        customDialog.setContentView(R.layout.widget_sheet_dialog);
        //取消按钮监听器
        customDialog.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) 
            {
                customDialog.stopDialog();
            }
        });
        return customDialog;
    }

    public static void startDialog(Context context, Map<String, View.OnClickListener> map) {
        startDialog(context, map, "");
    }
    /**
     * 开始
     * @param context
     * @param map
     * @param title
     */
    public static void startDialog(Context context, Map<String, View.OnClickListener> map, String title) 
    {
        stopDialog();
        if (customDialog == null)
        {
            customDialog = ActionSheet.createDialog(context);
        }
        
        Set<Map.Entry<String, View.OnClickListener>> set = map.entrySet();
        Iterator<Map.Entry<String, View.OnClickListener>> it = set.iterator();
        while (it.hasNext())
        {
            Map.Entry<String, View.OnClickListener> next = it.next();
            customDialog.addActionSheet(context, next.getKey(), next.getValue());
        }
        
        //按钮的长高
        int height = DensityUtil.dip2px(context, 50);
        int space = DensityUtil.dip2px(context, 1);
        WindowManager.LayoutParams params = customDialog.getWindow().getAttributes();
        //根据标题改变高度
        if (!TextUtils.isEmpty(title)) 
        {
            params.height = (map.size() + 1) * space + (map.size() + 1) * height + height;
        } 
        else 
        {
//            params.height = (map.size() + 1) * space + (map.size() + 1) * height;
            params.height = space + (map.size() + 1) * height;
        }
        params.width = (int) (customDialog.getWindow().getWindowManager().getDefaultDisplay().getWidth() * 1);

        //设置长宽
        customDialog.getWindow().setAttributes(params);
        setTitle(context, title);

        customDialog.show();
        customDialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;//R.anim.slide_up;
        //底部
        customDialog.getWindow().setGravity(Gravity.BOTTOM);
    }

    /**
     * 取消对话框
     */
    public static void stopDialog() 
    {
        if (customDialog != null) 
        {
            customDialog.dismiss();
            customDialog = null;
        }
    }

    /**
     * 设置标题
     * @param context
     * @param title
     */
    public static void setTitle(Context context, String title) 
    {
        if (!TextUtils.isEmpty(title)) 
        {
            ((TextView) customDialog.findViewById(R.id.title)).setText(title);
            customDialog.findViewById(R.id.title).setVisibility(View.VISIBLE);
//            //属性设置
//            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
//                    DensityUtil.dip2px(context, 40));
//            ((TextView) customDialog.findViewById(R.id.title)).setGravity(Gravity.CENTER);
//            /**
//             * 设置组件间距
//             */
//            customDialog.findViewById(R.id.title).setLayoutParams(params);
        }
    }

    /**
     * 添加组件及关联监听器
     * @param str：显示的名字
     * @param listener:监听
     */
    public void addActionSheet(Context context, String str, View.OnClickListener listener)
    {
        LayoutInflater inflater = LayoutInflater.from(context);
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.widget_sheet_button, null);
        Button actionButton = (Button) layout.findViewById(R.id.action_sheet_button);
        actionButton.setText(str);
        actionButton.setOnClickListener(listener);
        
        //添加组件
        
        ((LinearLayout) customDialog.findViewById(R.id.action_more)).addView(layout);
    }

}
